package com.awcall.iot.config;

import com.awcall.iot.common.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
public class GlobalExceptionHandler {

    private final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResponseResult jsonErrorHandler(Exception exception, HttpServletRequest request, HttpServletResponse response) throws IOException {
        log.error(request.getRequestURI(), exception);
        return new ResponseResult<>(500, "系统异常", exception.getMessage());
    }

}
